#include "simpleprogressnotifier.h"
#include <QDebug>

SimpleProgressNotifier::SimpleProgressNotifier(QObject* parent)
    : QObject(parent)
    , m_currentState(Idle)
    , m_currentProgress(0)
    , m_debounceTimer(new QTimer(this))
{
    m_debounceTimer->setSingleShot(true);
    m_debounceTimer->setInterval(DEBOUNCE_INTERVAL_MS);
    connect(m_debounceTimer, &QTimer::timeout, this, &SimpleProgressNotifier::onStateTimeout);
}

void SimpleProgressNotifier::setState(ProgressState state)
{
    if (m_currentState == state) {
        return; // 状态未变化
    }

    m_currentState = state;

    // 状态变化时自动重置进度
    if (state == Idle || state == Completed || state == Failed || state == Cancelled) {
        m_currentProgress = (state == Completed) ? 100 : 0;
    }


    emit stateChanged(state);
    emitUIUpdate();
}

void SimpleProgressNotifier::setProgress(int percentage)
{
    // 防抖：避免过于频繁的进度更新
    if (abs(percentage - m_currentProgress) < 2) {
        return; // 变化太小，忽略
    }

    m_currentProgress = qBound(0, percentage, 100);

    // 如果处于非活动状态，自动切换到处理中
    if (m_currentState == Idle && m_currentProgress > 0) {
        setState(Processing);
    }

    // 完成时自动切换状态
    if (m_currentProgress >= 100 && m_currentState == Processing) {
        setState(Completing);
    }

    emit progressUpdated(m_currentProgress);
    emitUIUpdate();
}

void SimpleProgressNotifier::reset()
{
    // 停止防抖定时器，确保不会有延迟的UI更新
    if (m_debounceTimer && m_debounceTimer->isActive()) {
        m_debounceTimer->stop();
    }
    
    setState(ProgressState::Idle);
    m_currentProgress = 0;
    m_lastEmittedText.clear();
    m_lastEmittedColor = QColor();
}

QString SimpleProgressNotifier::getStateText() const
{
    switch (m_currentState) {
    case Idle:
        return "就绪";
    case Preparing:
        return "准备中";
    case Processing:
        return QString("导出中 %1%").arg(m_currentProgress);
    case Completing:
        return "完成中";
    case Completed:
        return "已完成";
    case Failed:
        return "失败";
    case Cancelled:
        return "已取消";
    default:
        return "未知";
    }
}

QColor SimpleProgressNotifier::getStateColor() const
{
    switch (m_currentState) {
    case Idle:
        return QColor(120, 120, 120); // 灰色
    case Preparing:
        return QColor(52, 152, 219);  // 蓝色
    case Processing:
        return QColor(52, 152, 219);  // 蓝色
    case Completing:
        return QColor(46, 204, 113);  // 绿色
    case Completed:
        return QColor(46, 204, 113);  // 绿色
    case Failed:
        return QColor(231, 76, 60);   // 红色
    case Cancelled:
        return QColor(241, 196, 15);  // 橙色
    default:
        return QColor(120, 120, 120); // 灰色
    }
}

void SimpleProgressNotifier::onStateTimeout()
{
    emitUIUpdate();
}

void SimpleProgressNotifier::emitUIUpdate()
{
    QString newText = getStateText();
    QColor newColor = getStateColor();

    // 防抖：避免重复发送相同的UI更新
    if (m_lastEmittedText == newText && m_lastEmittedColor == newColor) {
        return;
    }

    m_lastEmittedText = newText;
    m_lastEmittedColor = newColor;

    // 延迟发送，避免过于频繁的UI更新
    m_debounceTimer->start();

    emit UIUpdateRequested(newText, newColor);
}